﻿package com.bizmlib.utils
{
	/**
	 * KeyValuePair is a data object that holds a key (String) and a corresponding 
	 * value (Object).
	 * 
	 * @author Valentyn Derkach
	 */
	class KeyValuePair
	{
	
		private var key:String;
		private var value:Object;
	
		/**
		 * Constructor
		 * @param key the key for this key value pair
		 * @param value the value for this key value pair
		 */
		function KeyValuePair(key:String, value:Object) {
			this.key = key;
			this.value = value;
		}
	
		/**
		 * Gets the key of this key value pair
		 * @return the key
		 */
		function getKey():String {
			return this.key;
		}
		
		/**
		 * Gets the value of this key value pair
		 * @return the value
		 */
		function getValue():Object {
			return this.value;
		}
	
	
		public function clone():KeyValuePair
		{
			return new KeyValuePair(getKey(), createCopyOf(getValue()));
		}
		private function createCopyOf(item:Object):Object
		{
			//trace("Map.createCopyOf(" + arguments + ")");
			//trace(typeof(item));
			//trace(item instanceof Function);
	
			if((typeof(item) == "object") && (typeof(item) != "function"))
			{
				if(item.clone)
				{
					return item.clone();
				}
				else
				{
					var copy:Object = new Object();
					for(var propertyName:String in item)
					{
						copy[propertyName] = createCopyOf(item[propertyName]);
					}
					return copy;
				}
			}
			else
			{
				return item;
			}
		}
	
	
		/**
		 * Returns a string representation of this key value pair
		 * @return the string in [key]=[value] format
		 */
		function toString():String {
			return getKey() + " -> " + getValue().toString();
		}
	}
}